/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
//import DAO.ConnectionFactory;
import Classes.Cliente;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Statement;
import javax.swing.JOptionPane;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author William
 */
public class ClienteDAO extends Conexao {
    
    private Connection connection;
  
    
   
    
    public void adiciona(Cliente cliente){
        
        
        try {
            Statement statement = null;
            String sql = "INSERT INTO  Cliente(id, nome, cpf, email, cidade, uf, telefone1, telefone2)"
                    + "VALUES (null, '"
                    + cliente.getNome() + "','"
                    + cliente.getCpf() + "','"
                    + cliente.getEmail() + "','"
                    + cliente.getCidade() + "','"
                    + cliente.getUf() + "','"
                    + cliente.getTelefone1() + "','"
                    + cliente.getTelefone2() + "')";
            statement = (Statement) abrirConexao().createStatement();
            statement.execute(sql);
            JOptionPane.showMessageDialog(null,"Cliente cadastrado com sucesso !");
                        
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,"Erro no banco de dados!");
        }
    }
    
    public List<Cliente>getLista(){
        try{
            
            PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("SELECT * FROM cliente ORDER BY id");
            ResultSet rs = stmt.executeQuery();
            List<Cliente> cliente = new ArrayList<Cliente>();
            
            while(rs.next()){
                Cliente cliente2 = new Cliente();
                cliente2.setId(Integer.parseInt(rs.getString("id")));
                cliente2.setNome(rs.getString("nome"));
                cliente2.setCpf(rs.getString("cpf"));
                cliente2.setEmail(rs.getString("email"));
                cliente2.setTelefone1(rs.getString("telefone1"));
                cliente2.setTelefone2(rs.getString("telefone2"));
                cliente2.setCidade(rs.getString("cidade"));
                cliente2.setUf(rs.getString("uf"));
                
            }
            
            stmt.close();
            connection.close();
            return cliente;
            
        }catch (SQLException e){
            
        }
        return null;
    }
    
    public void altera(Cliente cliente){
        
        String sql = "UPDATE cliente SET nome=?, cpf=?, email=?, telefone1=?, telefone2=?, cidade=?, uf=? WHERE Id=?";
        try{
            PreparedStatement stmt= (PreparedStatement) connection.prepareStatement(sql);
            
            stmt.setString(1, cliente.getNome());
            stmt.setString(2, cliente.getCpf());
            stmt.setString(3,cliente.getEmail());
            stmt.setString(4, cliente.getTelefone1());
            stmt.setString(5, cliente.getTelefone2());
            stmt.setString(6, cliente.getCidade());
            stmt.setString(7, cliente.getUf());
            stmt.executeQuery();
            stmt.close();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public void remove(Cliente cliente){
        try{
            PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("DELETE FROM cliente WHERE id = ?");
            
            stmt.setInt(1, cliente.getId());
            stmt.execute();
            stmt.close();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    
}
